<?php

/**
* Localization.class.php
*
* Localization class
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/

require_once "config.php";
require_once "Auth.php";


class LocalizationSingleton 
{

    function &instance() 
    {

        static $instance; 

        if(!isset($instance)) { 

            $instance = new LocalizationSingleton();
        }
        return $instance;
   }    

    private function LocalizationSingleton()
    {
        trigger_error("ENTER LocalizationSingleton::LocalizationSingleton()", E_USER_NOTICE);
        $this->setDefault();
        $this->setupEnv( $this->getLocale()  );
    }

    private function setDefault()
    {
        
        $options = &PEAR::getStaticProperty('CONFIG_INI','LOCALE');

        $this->locale_dir = $options['locale_dir'];
       
        if ( !isSet( $_COOKIE['locale'] )  || $_COOKIE['locale'] == ''  )
        {
            $httpLocale = $this->getDefaulHttpBasedLocale();
            $this->setCookie( strlen($httpLocale) ? $httpLocale : $options['default_locale']  );           
            trigger_error("ENTER LocalizationSingleton:setDefault() Default locale set : ".$_COOKIE['locale'], E_USER_NOTICE);
        }
        else
        {
            trigger_error("ENTER LocalizationSingleton:setDefault() Cookie locale : ".$_COOKIE['locale'], E_USER_NOTICE);
        }
        
    }

    function setCookie( $value ) 
    {
        setcookie('locale', $value ,time()+60*60*24*30 ) ; 
        $_COOKIE['locale'] = $value;
    }

    function setLocale( $locale )
    {
      

        if ( isSet($locale) && strlen($locale) )
        {   
            trigger_error("ENTER LocalizationSingleton:setLocale() Selected locale :".$locale, E_USER_NOTICE);                
            $this->setCookie ( $locale ) ;
            $this->setupEnv( $locale );
        }



    }

    function getLocale()
    {
        return $_COOKIE['locale'];
    }

    private function setupEnv( $locale = '' )
    {
        $locale = $locale.".utf8";
        putenv("LC_ALL=". $locale);
        trigger_error("ENTER LocalizationSingleton:setupEnv() getenv :'".getenv("LC_ALL")."'", E_USER_NOTICE); 

        $setlocale = setlocale(LC_ALL,  $locale);
        trigger_error("ENTER LocalizationSingleton:setupEnv() setlocale :'".$setlocale."'", E_USER_NOTICE); 

        $bindtextdomain = bindtextdomain("messages", $this->locale_dir);
        trigger_error("ENTER LocalizationSingleton:setupEnv() bindtextdomain :'".$bindtextdomain."'", E_USER_NOTICE); 


        $textdomain = textdomain("messages");
        trigger_error("ENTER LocalizationSingleton:setupEnv() textdomain :'".$textdomain."'", E_USER_NOTICE); 

        $bindTestdomainCodeset = bind_textdomain_codeset("messages", 'UTF-8');

        trigger_error("ENTER LocalizationSingleton:setupEnv() bind_textdomain_codeset :'".$bindTestdomainCodeset."'", E_USER_NOTICE); 
    }

    function getDefaulHttpBasedLocale()
    {
        $httpLocale = $this->getHttpLocale();

        if ( !count($httpLocale) )
        {
            return null;
        }
        $avaliableLocale = $this->getAvaliableLocale();

        foreach ( $httpLocale as $locale )
        {

            //Getonly language
            $locale = explode("_",$locale);
            $locale = $locale[0];

            foreach ( $avaliableLocale as $avaliable ) 
            {
                if ( strpos($avaliable,$locale) !==false )
                {
                    return $avaliable;
                }
            }   
        }
    }


    private function getHttpLocale()
    {
        if( !isSet($_SERVER['HTTP_ACCEPT_LANGUAGE']) )
        {
            return null;
        }

        $locale = explode(';',  $_SERVER['HTTP_ACCEPT_LANGUAGE']  );
        $locale =  explode(',', $locale[0]);
        //Now change "-" to "_"
        return str_replace("-","_",$locale);
    }

    function getAvaliableLocale()
    {
        if (is_dir($this->locale_dir)) 
        {
            $dh = opendir($this->locale_dir);

            while (($file = readdir($dh)) !== false) 
            {
                if ( $file !="." && $file !=".." && !strstr($file,'.') )
                {
                    $locale[] = $file;
                }
            }

        }
        else
        {
            trigger_error("Localization::getHttpLocale() Invalid locale directory ".$this->locale_dir ,E_USER_ERROR);
        }
        
        return $locale;
    }
    
}
?>
